package com.ycxy.sh.transaction.controller;


import com.ycxy.sh.transaction.dao.vo.LayuiPageVo;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.dao.entity.Notices;
import com.ycxy.sh.transaction.service.NoticesService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 *  消息通知控制器
 *
 * @author hlt
 * @since 2019-12-25
 */
@Slf4j
@Api(tags = "9.消息通知模块")
@RestController
public class NoticesController {
    @Autowired
    private NoticesService noticesService;

    /**
     * 用户查看通知消息后
     * 1.前端传入通知id（id）
     * 2.将其设置为已读
     * */
    @ResponseBody
    @PostMapping("/notices/look/{id}")
    public JsonResult selectNoticesById (@PathVariable("id") Long id) {
      noticesService.insertNotices(id);
      log.debug("通知消息设置失败");
      return JsonResult.ok();
    }

    /**
     *查询前10条公告
     * **/
    @ResponseBody
    @GetMapping("/notices/queryNotices")
    public JsonResult queryNotices (HttpSession session){
        List<Notices> noticesList = noticesService.queryNotices(session);
        log.debug("公告查询成功");
        return JsonResult.ok(noticesList);
    }

    /**
     * 取消新通知标志
     * 用户点击查看最新通知后会将所有通知设置为非最新通知
     * */
    @ResponseBody
    @GetMapping("/notices/cancelLatest")
    public JsonResult CancelLatest (HttpSession session){
        noticesService.CancelLatest(session);
        log.debug("新通知消息取消成功！");
        return JsonResult.ok();
    }

    /**
     * 分类分页查询用户所有通知消息
     * 1.前端传入消息通知类型（noticeType）
     * 2.session中获取用户id（userid）
     * 3.返回分页数据
     * */
    @ResponseBody
    @GetMapping("/notices/queryall")
    public LayuiPageVo queryallSold(int limit, int page, HttpSession session) {
        Long userid = (Long) session.getAttribute("userid");
        List<Notices> noticesList = noticesService.pageUserAllNotices((page - 1) * limit, limit, userid);
        Integer dataNumber = noticesService.queryNoticesCount(userid);
        return new LayuiPageVo("", 0,dataNumber,noticesList);
    }

}

